<?php
class Customers_Model extends CI_Model
{
    function __construct() 
    {
        parent::__construct();
    }
    private function _set_where_conditions($options = array())
    {
        if (isset($options['id']))
            $this->db->where(CUSTOMERS.'.id', $options['id']);

        if(isset($options['name']) && $options['name'] != '')
            $this->db->like('name', $options['name']);
        
        if(isset($options['code']) && $options['code'] != '')
            $this->db->like('code', $options['code']);
        
        if(isset($options['lg_code']) && $options['lg_code'] != '')
            $this->db->where('code', $options['lg_code']);
        
        if(isset($options['password']) && $options['password'] != '')
            $this->db->where('password', $options['password']);
        
        if(isset($options['phone']) && $options['phone'] != '')
            $this->db->like('phone1', $options['phone']);
        
        if(isset($options['address']) && $options['address'] != '')
            $this->db->like('address', $options['address']);
        
        if(isset($options['email']) && $options['email'] != '')
            $this->db->like('email', $options['email']);

        if (isset($options['active']) && $options['active'] != '')
            $this->db->where('active', $options['active']);
        
        

        if (isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);
        else if (isset($options['limit']))
            $this->db->limit($options['limit']);
        if(isset($options['order_by']))
            $this->db->order_by($options['order_by']);
        else
            $this->db->order_by('created_date desc');
    }
    function get_customers($options = array())
    {
        $this->_set_where_conditions($options);

        $query = $this->db->get(CUSTOMERS);
        if(isset($options['last_row']) || isset($options['id']))
            return $query->row(0);
        return $query->result();
    }

    function get_customers_array($options = array())
    {
        $customers = $this->get_customers($options);
        $output = array();
        foreach($customers as $customer)
        {
            $output[$customer->id] = $customer->name;
        }
        return $output;
    }

    function get_customers_combo($options = array())
    {
        // Default categories name
        if ( ! isset($options['combo_name']))
        {
            $options['combo_name'] = CUSTOMERS;
        }

        if ( ! isset($options['extra']))
        {
            $options['extra'] = '';
        }

        $customers = $this->get_customers($options); // added options 2011-03-11

        $data_options = array();

        if (isset($options['ALL']))
            $data_options[DEFAULT_COMBO_VALUE] = 'Tất cả';

        foreach($customers as $customer)
        {
            if (isset($options['FULL']))
                $data_options[$customer->id] = $customer->fullname;
            else
                $data_options[$customer->id] = $customer->alias_name;
        }

        if ( ! isset($options[$options['combo_name']]))
        {
            $options[$options['combo_name']] = DEFAULT_COMBO_VALUE;
        }
        return form_dropdown($options['combo_name'], $data_options, $options[$options['combo_name']], $options['extra']);
    }

    function add_customer($data = array())
    {
        $this->db->insert(CUSTOMERS, $data);
    }

    function update_customer($data = array())
    {
        if (isset($data['id']))
        {
            $this->db->where(CUSTOMERS . '.id', $data['id']);
            $this->db->update(CUSTOMERS, $data);
        }
    }

    function delete_customer($customers_id)
    {
        if (isset($customers_id))
        {
            $this->db->where(CUSTOMERS . '.id', $customers_id);
            $this->db->delete(CUSTOMERS);
        }
    }
    
    public function get_customers_count($options = array()) {
        return count($this->get_customers($options));
    }
    
    private function _set_where_conditions1($options = array())
    {
        if (isset($options['id']))
            $this->db->where(CUSTOMERS_FILES.'.id', $options['id']);
        
        if (isset($options['customer_id']))
            $this->db->where(CUSTOMERS_FILES.'.customer_id', $options['customer_id']);

        if(isset($options['title']) && $options['title'] != '')
            $this->db->like('title', $options['title']);
        
        if (isset($options['status']) && $options['status'] != '')
            $this->db->where('status', $options['status']);

        if (isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);
        
        else if (isset($options['limit']))
            $this->db->limit($options['limit']);
        
        if(isset($options['order_by']))
            $this->db->order_by($options['order_by']);
        else
            $this->db->order_by('created_date desc');
    }
    
    function get_customer_files($options = array())
    {
        $this->_set_where_conditions1($options);
        $query = $this->db->get(CUSTOMERS_FILES);
        if(isset($options['last_row']) || isset($options['id']))
            return $query->row(0);
        return $query->result();
    }
    
    public function get_customers_files_count($options = array()) {
        return count($this->get_customer_files($options));
    }
    
    function add_customer_file($data = array())
    {
        $this->db->insert(CUSTOMERS_FILES, $data);
    }

    function update_customer_file($data = array())
    {
        if (isset($data['id']))
        {
            $this->db->where(CUSTOMERS_FILES . '.id', $data['id']);
            $this->db->update(CUSTOMERS_FILES, $data);
        }
    }
    
    function delete_customer_file($file_id)
    {
        if (isset($file_id))
        {
            $this->db->where(CUSTOMERS_FILES . '.id', $file_id);
            $this->db->delete(CUSTOMERS_FILES);
        }
    }
}
